package com.antis.olsl.magic.analysis;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.antis.olsl.base.BaseFragment;
import com.antis.olsl.base.utils.ToastUtil;
import com.antis.olsl.magic.databinding.FragmentAnalysisBinding;
import com.antis.olsl.magic.response.GetSeasonsAnalysisResp;
import com.trello.rxlifecycle2.LifecycleTransformer;

import java.util.HashMap;
import java.util.Map;

import timber.log.Timber;

/**
 * 季节分析Fragment
 */
public class AnalysisFragment extends BaseFragment<FragmentAnalysisBinding> implements AnalysisContract.View {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_PARAM3 = "param3";

    private String mShopId;
    private int mYear;
    private int mType;

    private AnalysisPresenter mPresenter;

    public AnalysisFragment() {
        // Required empty public constructor
    }

    public static AnalysisFragment newInstance(String salesRoomIds, int year, int type) {
        AnalysisFragment fragment = new AnalysisFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, salesRoomIds);
        args.putInt(ARG_PARAM2, year);
        args.putInt(ARG_PARAM3, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mShopId = getArguments().getString(ARG_PARAM1);
            mYear = getArguments().getInt(ARG_PARAM2);
            mType = getArguments().getInt(ARG_PARAM3);
        }
    }

    @Override
    protected void initView(View view) {
        initPresenter();
        switch (mType) {
            case 1:
                viewBinding.textLabel.setText("春季");
                break;
            case 2:
                viewBinding.textLabel.setText("夏季");
                break;
            case 3:
                viewBinding.textLabel.setText("秋季");
                break;
            case 4:
                viewBinding.textLabel.setText("冬季");
                break;
            default:
                break;
        }
    }

    private void initPresenter() {
        mPresenter = new AnalysisPresenter();
        mPresenter.takeView(this);
    }

    @Override
    protected void loadData() {
        loadData(mShopId, mYear, mType);
    }

    @Override
    protected FragmentAnalysisBinding getViewBinding() {
        return FragmentAnalysisBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    public void loadData(String shopId, int year, int type) {
        Map<String, Object> map = new HashMap<>(3);
        map.put("shopId", shopId);
        map.put("year", year);
        map.put("tag", type);
        mPresenter.getSeasonsAnalysis(map);
    }

    @Override
    public void getSeasonsAnalysisSuccess(GetSeasonsAnalysisResp.ContentBean bean) {
        if (null != bean) {
            if (!TextUtils.isEmpty(bean.salesTime)) {
                viewBinding.textNumber.setText(bean.salesTime);
            }
            if (!TextUtils.isEmpty(bean.salesTimePercent)) {
                viewBinding.textNumberProportion.setText(bean.salesTimePercent);
            }
            if (!TextUtils.isEmpty(bean.xssl)) {
                viewBinding.textAmount.setText(bean.xssl);
            }
            if (!TextUtils.isEmpty(bean.xsslzb)) {
                viewBinding.textAmountProportion.setText(bean.xsslzb);
            }
            if (!TextUtils.isEmpty(bean.xsje)) {
                viewBinding.textMoney.setText(bean.xsje);
            }
            if (!TextUtils.isEmpty(bean.xsjezb)) {
                viewBinding.textMoneyProportion.setText(bean.xsjezb);
            }
            if (!TextUtils.isEmpty(bean.xsml)) {
                viewBinding.textProfit.setText(bean.xsml);
            }
            if (!TextUtils.isEmpty(bean.xsmlzb)) {
                viewBinding.textProfitProportion.setText(bean.xsmlzb);
            }
        }
    }

    @Override
    public void getSeasonsAnalysisFailure(String msg) {
        Timber.tag("hhh").e("getSeasonsAnalysisFailure");
        if (TextUtils.isEmpty(msg)) {
            msg = "请求失败,请稍后重试";
        }
        ToastUtil.showToast(this.getContext(), msg);
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return this.bindToLifecycle();
    }
}
